package com.huilian.iotbox.admin.wap.controller;


import com.huilian.iotbox.admin.wap.aopLog.OperateLog;
import com.huilian.iotbox.admin.wap.service.DeviceService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.DeviceDto;
import com.huilian.iotbox.data.enums.DeviceTypeEnum;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.vo.DeviceVo;
import com.huilian.iotbox.data.vo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 谢洛涛
 * @date 2021/1/9 11:33
 */
@Slf4j
@RestController
@RequestMapping("/api/device")
public class DeviceController extends UserInfoController {
    @Autowired
    private DeviceService deviceService;

    @PostMapping("/updateBindDeviceByMerchantStoreId")
    public ServerResponse updateBindDeviceByMerchantStoreId(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserId(userInfo.getId());
        deviceDto.setAgencyId(userInfo.getAgencyId());
        return deviceService.updateBindDeviceByMerchantStoreId(deviceDto, userInfo);
    }

    @PutMapping("/updateUnbindDevice")
    public ServerResponse updateUnbindDevice(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        deviceDto.setUserId(userInfo.getId());
        return deviceService.updateUnbindDevice(deviceDto);
    }

    @GetMapping("/findOneDeviceByIdAndPermission")
    public ServerResponse findOneDeviceByIdAndPermission(DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(deviceService.findOneDeviceByIdAndPermission(deviceDto));
    }


    /**
     * 查询当前账号设备关联的厂家有没有自定义公众号
     *
     * @param request
     * @return
     */
    @GetMapping("/getDeviceByManufacturers")
    public ServerResponse getDeviceByManufacturers(HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return deviceService.getDeviceByManufacturers(userInfo);
    }


    /**
     * 转移设备
     */
    @PostMapping("/shiftDevice")
    public ServerResponse shiftDevice(HttpServletRequest request, @RequestBody DeviceDto deviceDto) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return deviceService.shiftDevice(deviceDto);
    }


    /**
     * 根据场地数组查出对应的设备
     *
     * @param request
     * @param deviceDto
     * @return
     */
    @PostMapping("/findAllDeviceByMerchantStoreIdArray")
    public ServerResponse findAllDeviceByMerchantStoreIdArray(HttpServletRequest request, @RequestBody DeviceDto deviceDto) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return deviceService.findAllDeviceByMerchantStoreId(deviceDto);
    }

    @OperateLog
    @PutMapping("/updateDeviceRemarkNameById")
    public ServerResponse updateDeviceRemarkNameById(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return deviceService.updateDeviceRemarkNameById(deviceDto);
    }


    /**
     * 根据条件查集合
     *
     * @param request
     * @param deviceDto
     * @return
     */
    @GetMapping("/findAllDeviceByCondition")
    public ServerResponse findAllDeviceByCondition(HttpServletRequest request, DeviceDto deviceDto) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return ServerResponse.createBySuccess(deviceService.findAllDeviceByCondition(deviceDto));
    }


    @GetMapping("/findAllDeviceByConditionAndPage")
    public ServerResponse findAllDeviceByConditionAndPage(DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserId(userInfo.getId());
        deviceDto.setUserInfo(userInfo);
        return deviceService.findAllDeviceByConditionAndPage(deviceDto);
    }

    @GetMapping("/findVendingMachineByConditionAndPage")
    public ServerResponse findStoreAndBindingDeviceList(DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserId(userInfo.getId());
        deviceDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(deviceService.findVendingMachineByConditionAndPage(deviceDto));

    }


    @GetMapping("/findStatisticsDevice")
    public ServerResponse findStatisticsDevice(DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserId(userInfo.getId());
        deviceDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(deviceService.findStatisticsDevice(deviceDto));
    }

    @GetMapping("/findVendingMachineDevice")
    public ServerResponse findVendingMachineDevice(DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserId(userInfo.getId());
        deviceDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(deviceService.findVendingMachineDevice(deviceDto));
    }

    @PostMapping("/updateDeviceParamsById")
    public ServerResponse updateDeviceParamsById(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserId(userInfo.getId());
        return deviceService.updateDeviceParamsById(deviceDto);
    }


    @PostMapping("/findDevice")
    public ServerResponse findDevice(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return deviceService.findDevice(deviceDto);
    }

    @PostMapping("/statisticsOnlineDevice")
    public ServerResponse statisticsOnlineDevice(HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return deviceService.statisticsOnlineDevice(userInfo);
    }

    /***
     * 设备排行榜.
     * @param deviceDto
     * @param request
     * @return
     */
    @PostMapping("/findDeviceRankingList")
    public ServerResponse findDeviceRankingList(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
//        UserInfo userInfo = super.getLoginUserInfo(request);
//        deviceDto.setUserInfo(userInfo);
//        return ServerResponse.createBySuccess(deviceService.findDeviceRankingList(deviceDto));
        //未修改sql使用时间函数
        return ServerResponse.createBySuccess();
    }

    /**
     * 生成设备二维码
     *
     * @param request
     * @param deviceDto
     * @return
     */
    @GetMapping("/findClientUrl")
    public ServerResponse findClientUrl(HttpServletRequest request, DeviceDto deviceDto) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return deviceService.findClientUrl(deviceDto);
    }

    /**
     * 查询设备是否已经绑定
     *
     * @return
     */
    @GetMapping("/findIsBinding")
    public ServerResponse findIsBinding(String deviceNo, HttpServletRequest request) {
        if (StringUtils.isEmpty(deviceNo)) {
            return ServerResponse.createByError("设备号有误,请联系平台");
        }
        DeviceDto deviceDto = new DeviceDto();
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        deviceDto.setDeviceNo(deviceNo);
        return deviceService.findIsBinding(deviceDto);
    }

    @GetMapping("/findBindingDevice")
    public ServerResponse findBindingDevice(HttpServletRequest request) {
        DeviceDto deviceDto = new DeviceDto();
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(deviceService.findBindingDevice(deviceDto));
    }

    @PostMapping("/list")
    public ServerResponse deviceList(HttpServletRequest request, @RequestBody DeviceDto deviceDto) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(deviceService.deviceList(deviceDto));
    }

    @GetMapping("/findOneDeviceByDeviceNo")
    public ServerResponse findOneDeviceByDeviceNo(DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);

        if (org.springframework.util.StringUtils.isEmpty(deviceDto.getDeviceNo())) {
            return ServerResponse.createBySuccess("请输入设备号");
        }
        if (!ObjectUtils.isEmpty(deviceDto.getDeviceTypeId()) && deviceDto.getDeviceTypeId().equals(DeviceTypeEnum.VENDING_MACHINE.getCode())) {
            if (!ObjectUtils.isEmpty(deviceDto.getProductId())) {
                return ServerResponse.createBySuccess(deviceService.findOneDeviceByDeviceNoJoinProduct(deviceDto.getDeviceNo()));
            } else {
                return ServerResponse.createByError("请检查设备供电、串口连接是否正常");
            }
        } else {
            return ServerResponse.createBySuccess(deviceService.findOneDeviceByDeviceNo(deviceDto.getDeviceNo()));
        }
    }
    /**
     * 禁用/解除禁用 单个设备
     */
    @PostMapping("/upDisableDevice")
    public ServerResponse upDisableDevice(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return deviceService.upDisableDevice(deviceDto);
    }

    /**
     * 单设备转场
     */
    @PostMapping("/deviceTransition")
    public ServerResponse deviceTransition(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return deviceService.deviceTransition(deviceDto);
    }

    /**
     * 商品分类开关
     */
    @PostMapping("/updateCommodityGroupSwitch")
    public ServerResponse updateCommodityGroupSwitch(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(deviceService.updateCommodityGroupSwitch(deviceDto));
    }

    /**
     * 修改顾客购买设置
     */
    @PostMapping("/updateCustomerBuySet")
    public ServerResponse updateCustomerBuySet(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(deviceService.updateCustomerBuySet(deviceDto));
    }


    @GetMapping("/bluetoothTestResults")
    public ServerResponse bluetoothTestResults(String protocol) {
        deviceService.bluetoothTestResults(protocol);
        return ServerResponse.createBySuccess();
    }

    @PostMapping("/bluetoothOneClickReplenishment")
    public ServerResponse bluetoothOneClickReplenishment(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        deviceDto.setUserId(userInfo.getId());
        return deviceService.bluetoothOneClickReplenishment(deviceDto);
    }

    @PostMapping("/findOutOfStockItems")
    public ServerResponse findOutOfStockItems(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        deviceDto.setUserInfo(userInfo);
        deviceDto.setUserId(userInfo.getId());
        return deviceService.findOutOfStockItems(deviceDto);
    }

}
